package com.supergzt.controller;

import com.supergzt.annotation.RequiresLogin;
import com.supergzt.context.SecurityContextHolder;
import com.supergzt.entity.base.Result;
import com.supergzt.entity.dos.User;
import com.supergzt.entity.vos.UserPasswordVo;
import com.supergzt.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 张钧
 * @Description
 * @create 2023-03-29 16:28
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	@GetMapping("info")
	@RequiresLogin
	public Result<?> getUserInfo(){
		return Result.success(userService.getUserInfo());
	}


	@PutMapping ("update")
	@RequiresLogin
	public Result<?> updateUser(@RequestBody User user) {
		userService.updateById(user.setPassword(null).setUserId(SecurityContextHolder.getUserId()));
		userService.forceLogout();
		return Result.ok();
	}

	@PutMapping ("password")
	@RequiresLogin
	public Result<?> updateUserPassword(@RequestBody UserPasswordVo userPasswordVo) {
		userService.updateUserPassword(userPasswordVo);
		userService.forceLogout();
		return Result.ok();
	}


}
